package com.drcoffee.drcoffeemall.mapper;

import com.drcoffee.drcoffeemall.entry.*;
import com.drcoffee.drcoffeemall.entry.search.SearchUser;
import com.drcoffee.drcoffeemall.entry.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Mapper
@Repository
public interface UserMapper {

    @Select("select * from t_user where account = #{account}")
    List<User> getByAccount(String account);

    /* 修改用户信息 */
    boolean uploadUserFirm(User user);

    @Insert("INSERT INTO t_invitation_code(code,firm_name,invoice_title,invoice_number,discount_one,discount_two,erp_code) " +
            "VALUES ( #{code},#{firm_name},#{invoice_title},#{invoice_number},#{discount_one},#{discount_two},#{erp_code})")
    void addInvitation(InvitationCode invitationCode);

    List<Map<String, Object>> getInvitationCodeData( Map<String, Object> paramMap);

    @Delete("DELETE FROM t_invitation_code WHERE id = #{id} AND status = 'N'")
    void deleteInvitation(Integer id);

    @Select("SELECT a.*,b.erp_code AS erp_code,b.firm_name AS old_firm_name,b.Invoice_title AS old_invoice_title,b.Invoice_number AS old_invoice_number FROM t_firm_alter a LEFT JOIN t_user b ON a.user_account = b.account WHERE a.status = 0")
    List<FirmAlter> getFirmAlterByN();

    @Update("UPDATE t_user SET firm_name = #{firm_name},Invoice_title = #{invoice_title},Invoice_number = #{invoice_number} WHERE account = #{user_account}")
    void setUserFirm(FirmAlter firmAlter);

    @Update("UPDATE t_firm_alter SET status = #{status},remark = #{remark}, approver = #{approver},approver_date = CURRENT_TIMESTAMP WHERE id = #{id}")
    void setFirmAlter(FirmAlter firmAlter);

    @Insert("INSERT INTO t_user(email,firm_name,permission_id,name,country,erp_code,bs_user_id,bs_user_email) " +
            "VALUES ( #{email},#{firm_name},2,#{name},#{country},#{erp_code},#{bs_user_id},#{bs_user_email})")
    void signinOverseasUser(User user);

    @Select("SELECT *  FROM t_user WHERE permission_id = #{permission_id} AND (erp_code LIKE #{search} OR email LIKE #{search} OR phone LIKE #{search} OR firm_name LIKE #{search} OR username LIKE #{search})")
    List<User> getUserBySearch(Integer permission_id, String search);

    @Select("SELECT a.*,b.remark AS pric_namech FROM t_user a LEFT JOIN t_price_discount b ON a.price_discount_id = b.id WHERE a.permission_id=#{permission_id}")
    List<User> getUserByPermission(Integer permission_id);

    @Select("SELECT erp_code FROM t_user WHERE account = #{account}")
    String getUserErpCode(String account);

    @Select("SELECT a.*,CONCAT(b.o_file_path, b.folder, a.image_url) as file_path  FROM t_feedback a,t_file_path b where a.status = 'N' AND b.bsh_one = 'I' AND b.bsh_two = '1'")
    List<Feedback> getFeedback();

    @Update("UPDATE t_feedback SET status = 'Y' WHERE id = #{id}")
    void outFeedback(Integer id);

    @Update("UPDATE t_user SET discount_one = #{one} ,discount_two = #{two} WHERE account = #{account}")
    void setUserDiscount(String account, Double one, Double two);

    @Select("SELECT * FROM t_user WHERE account = #{account}")
    User getUserByAccount(String account);

    @Select("SELECT COUNT(0) FROM t_invitation_code WHERE code = #{code}")
    Integer getInvitationExistByCode(String code);

    @Select("SELECT COUNT(0) FROM t_invitation_code WHERE erp_code = #{erpCode} AND erp_code <> '552204001'")
    Integer getInvitationByErpCode(String erpCode);

    List<Map<String, Object>> getUserData(Map<String, Object> paramMap);

}
